<?php
/**
 * prime/load-index.phtml
 * Chargement Ajax de la liste des primes
 */
?>

<div class='list-container'>
    
    <table class="table table-striped list-table">
        <thead>
	    <tr>
		<td width='25%' style='text-align: center; font-weight: bold'>Libellé</td>
		<td width='25%' style='text-align: center; font-weight: bold'>Valeur</td>
		<td width='30%' style='text-align: center; font-weight: bold'>Description</td>
                <td width='10%' style='text-align: center; font-weight: bold'>CP / Hors CP</td>
		<td width='10%'>&nbsp;</td>
	    </tr>
            
            <tbody>                
                <?php foreach ($primes as $prime) : ?> 
                <tr>
                    <td style='text-indent: 10px'>
                        <a  class='edit-libelle' data-pk='<?php echo $prime->id; ?>' data-name='libelle' data-type='text' ><?php echo $prime->libelle ?></a>
                    </td>
                    
                    <td style='text-align: center;'>
                         <a  class='edit-valeur' data-pk='<?php echo $prime->id; ?>' data-name='valeur' data-type='text' ><?php echo $prime->valeur ?></a>
                    </td>
                    <td style='text-align: center;'>
                        <a  class='edit-description' data-pk='<?php echo $prime->id; ?>' data-name='description' data-type='textarea' ><?php echo $prime->description ?></a>
                    </td>
                    <td style='text-align: center'>                        
                        <a class='edit-is_cp' href='#' data-pk='<?php echo $prime->id; ?>' data-name='is_cp' data-type='select'>
                            <?php echo $prime->is_cp ? 'CP': 'Hors CP'?>
                        </a>
                    </td>
                    
                    <td>
                         <a href='#' data-pk="<?php echo $prime->id; ?>" data-libelle="<?php echo $prime->libelle; ?>" class="delete-prime">
                            <img src='<?php echo $this->basePath() . '/img/rhx/delete-16x16.png' ?>' alt='Effacer' title='Effacer'>
                        </a>
                        
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
	</thead>
    </table>
    
   
</div>

<script type='text/javascript'>
    $(function(){
        
        $('.edit-libelle').each(function(){
            $its = $(this);
            
            $its.editable({
                    title: 'Edition du libellé',
                    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'prime', 'action'	 => 'updateStringData')); ?>'
                }
                );
        });
        $('.edit-description').each(function(){
            $its = $(this);
            
            $its.editable({
                    title: 'Edition de la decription',
                    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'prime', 'action'	 => 'updateStringData')); ?>'
                }
                );
        });
        
        $('.edit-valeur').each(function(){
            $its = $(this);
            
            $its.editable({
                    title: 'Edition de la valeur',
                    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'prime', 'action'	 => 'updateNumberData')); ?>',
                    validate: function(val){
                        var val = $.trim(val),               
                            patFloat = new RegExp(/^[0-9]*[.]{0,1}[0-9]+$/);

                        if(! patFloat.test(val))
                            return 'Donnée invalide.';
                    }
                }
                );
        });
        
        
         $('.edit-is_cp').each(function(){
            $its = $(this);
            
            $its.editable({
                    title: 'CP / Hors CP',
                    source: [{value: 1, text: "CP"}, {value: 0, text: "Hors CP"}],
                    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'prime', 'action'	 => 'updateNumberData')); ?>',
                    validate: function(val){                       
                    }
                }
                );
        });
        
        $('.delete-prime').on('click', function(e){
            $this = $(this);
            e.preventDefault();
            pk      = $this.data('pk');
            libelle = $this.data('libelle');
            
            if (!confirm("Voulez-vous vraiment effacer la prime " + libelle + " ?")){
                return false;
            }
            var urlDelete = "<?php echo $this->url('RHX/default', array('controller'	 => 'prime', 'action'	 => 'erase')) ?>";
            
            ajax = $.ajax({
                type: 'post',
                url: urlDelete,
                data: '&pk=' + pk
            });
            ajax.success(function(result) {
                if(!result.done){
                    alert("Une erreur est survenue lors de la suppression de la prime !");
                }
                $this.parent().parent().hide('slow', function(){$this.remove()});
            });
            ajax.error(function() {
                alert('Une erreur est survenue');
            });
            return false;
        });
    });
</script>